package net.schmurgon.gwt.scaffold;

import com.google.gwt.app.place.Activity;
import com.google.gwt.app.place.ActivityMapper;
import com.google.gwt.app.place.Place;
import com.google.gwt.app.place.PlaceController;
import com.google.gwt.app.place.ProxyListPlace;
import net.schmurgon.gwt.request.ApplicationRequestFactory;
import net.schmurgon.gwt.request.ApplicationEntityTypesProcessor;
import net.schmurgon.gwt.ui.SeriesListActivity;
import net.schmurgon.gwt.request.SeriesRecord;
import net.schmurgon.gwt.ui.RunnerListActivity;
import net.schmurgon.gwt.request.RunnerRecord;
import net.schmurgon.gwt.ui.RunListActivity;
import net.schmurgon.gwt.request.RunRecord;
import net.schmurgon.gwt.ui.CourseListActivity;
import net.schmurgon.gwt.request.CourseRecord;

/**
 * Instantiates master activities.
 */
public final class ApplicationMasterActivities implements ActivityMapper {
	private final ApplicationRequestFactory requests;
	private final PlaceController placeController;

	public ApplicationMasterActivities(ApplicationRequestFactory requests, PlaceController placeController) {
  		this.requests = requests;
		this.placeController = placeController;
	}

	public Activity getActivity(Place place) {
		if (!(place instanceof ProxyListPlace)) {
			return null;
		}

		ProxyListPlace listPlace = (ProxyListPlace) place;

		return new ApplicationEntityTypesProcessor<Activity>() {
			@Override
			public void handleSeries(SeriesRecord isNull) {
				setResult(new SeriesListActivity(requests, placeController));
			}
			@Override
			public void handleRunner(RunnerRecord isNull) {
				setResult(new RunnerListActivity(requests, placeController));
			}
			@Override
			public void handleRun(RunRecord isNull) {
				setResult(new RunListActivity(requests, placeController));
			}
			@Override
			public void handleCourse(CourseRecord isNull) {
				setResult(new CourseListActivity(requests, placeController));
			}
		}.process(listPlace.getProxyClass());
	}
}
